﻿namespace LizardChess

module Mov =
    let create(mvpiece,mvpcname,from,mto,piececaptured,mvname,fiftymovedrawcounter,pgn,uci) =
        {
            mvPiece = mvpiece;
            mvPcName = mvpcname;
            From = from;
            To = mto;
            pieceCaptured = piececaptured;
            mvName = mvname;
            FiftyMoveDrawCounter = fiftymovedrawcounter;
            PGN = pgn;
            UCI = uci;
        }
    let blank = create(-1,enmName.Null,-1,-1,-1,CastleKingSide,-1,"","")
    let Isw(m) = if m.mvPiece<16 then true else false 
    let PGN(mv,dup,incheckmate,incheck) =
        let part1 =
            if mv.mvName = CastleKingSide then
                "O-O"
            elif mv.mvName = CastleQueenSide then
                "O-O-O"
            else
                let strpc = if mv.mvPcName <> enmName.Pawn then Piece.abbrev(mv.mvPcName) else ""
                let strfrom = Ref.sq.[mv.From]
                let strcap = if (mv.pieceCaptured <> -1) then "x" else ""
                let strto = Ref.sq.[mv.To]
                let strfrom2 = 
                    if dup<>"" && dup.Substring(0,1)=strfrom.Substring(0,1) then
                        strfrom.Substring(1,1)
                    elif dup<>"" then
                        strfrom.Substring(0,1)
                    elif mv.mvPcName=enmName.Pawn && strcap="x" then
                        strfrom.Substring(0,1)
                    else
                        ""
                strpc+strfrom2+strcap+strto
        let part2 = match mv.mvName with
                    |PawnPromotionQueen -> "=Q"
                    |PawnPromotionRook -> "=R"
                    |PawnPromotionKnight -> "=N"
                    |PawnPromotionBishop -> "=B"
                    |_ -> ""
        let part3 =
            if incheckmate then "#"
            elif incheck then "+"
            else ""
        part1+part2+part3

    /// coord - suppport function to convert move parts to string
    let coord sq = Ref.f.[sq] + Ref.r.[sq]
    /// uci - suppport function to convert move to uci string
    let uci mv = (coord mv.From) + (coord mv.To) +
                 (if mv.mvName=oMoveName.PawnPromotionBishop then "b"
                  elif mv.mvName=oMoveName.PawnPromotionKnight then "n"
                  elif mv.mvName=oMoveName.PawnPromotionRook then "r"
                  elif mv.mvName=oMoveName.PawnPromotionQueen then "q"
                  else "")

    /// fromuci - suppport function to convert uci string to move bits
    let fromuci (c:string) = 
        let f =Ref.ord (c.[0],c.[1])
        let t = Ref.ord (c.[2],c.[3])
        let mn = 
            if c.Length=4 then oMoveName.NullMove
            elif c.[4]='b' then oMoveName.PawnPromotionBishop
            elif c.[4]='n' then oMoveName.PawnPromotionKnight
            elif c.[4]='r' then oMoveName.PawnPromotionRook 
            else oMoveName.PawnPromotionQueen
        f,t,mn





